package com.umeox.watch.alphabetlearning.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.ColorDrawable;
import android.media.AudioManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.umeox.moto.watch.themes.util.ThemeUtils;
import com.umeox.moto.watch.themes.widget.Slider;
import com.umeox.watch.alphabetlearning.R;


public class VolumeSettingDialog extends Dialog implements DialogInterface.OnShowListener, Slider.OnPositionChangeListener {

    private Slider slVolume;
    private Context mContext;
    private AudioManager mAudioManager;

    public VolumeSettingDialog(Context context) {
        super(context);
        mContext = context;
        createDialog();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void createDialog() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_volume_settings, null);
        setContentView(view);
        slVolume = view.findViewById(R.id.slVolume);

        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        int mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        slVolume.setValueRange(0, mMaxVolume, false);
        slVolume.setValue(mCurrentVolume, false);

        slVolume.setOnPositionChangeListener(this);
        slVolume.onchangeState(true);
        setOnShowListener(this);

        Window window = getWindow();
        window.setBackgroundDrawable(new ColorDrawable(ThemeUtils.windowBackground(getContext(), 0x35454D)));
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.Animation_Uwear_Dialog_SlideVertical);
        WindowManager.LayoutParams params = window.getAttributes();
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
    }

    @Override
    public void onShow(DialogInterface dialog) {
        int mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        int mCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        slVolume.setValueRange(0, mMaxVolume, false);
        slVolume.setValue(mCurrentVolume, false);

    }

    @Override
    public void onPositionChanged(Slider view, boolean fromUser, float oldPos, float newPos, int oldValue, int newValue) {
        if (mAudioManager != null) {
            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newValue, 0);
        }
    }
}
